extends IBasicComponent


class_name INodeFactory
"""
用于批量产生和回收对象
"""


## 挂载结点
@export var mount_node: Node = null

@export_category("Recovery")
## 是否启用回收机制
@export var is_recovery_enable: bool = false
## 初始化时设置的属性
@export var create_values: Dictionary[String, Variant] = {}
## 删除时设置的属性
@export var destroy_values: Dictionary[String, Variant] = {}
@export_category("")


var __cached_pool: Array[Node] = []


# 生成一个对象(子类继承)
func __make_one() -> Node:
    return null

# 生成一个对象
func create() -> Node:
    var node: Node = null
    if is_recovery_enable and len(__cached_pool) > 0:
        node = __cached_pool[0]
        __cached_pool.pop_at(0)
        for field in create_values:
            node.set(field, create_values[field])
    else:
        node = __make_one()
    if not node:
        return null
    if mount_node:
        NodeHelper.add_child(mount_node, node)
    return node

# 销毁一个对象
func destroy(node: Node) -> void:
    if is_recovery_enable:
        for field in destroy_values:
            node.set(field, destroy_values[field])
        __cached_pool.append(node)
    else:
        node.queue_free()
